package com.vanysoft.business.data.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.data.entity.CategoryList;
import com.vanysoft.business.data.entity.CategoryManager;
import com.vanysoft.business.data.service.CategoryManagerService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.utils.JSONResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 品类经理表 前端控制器
 * </p>
 *
 * @author YangLei
 * @since 2018-07-09
 */
@Controller
@RequestMapping("/data/categoryManager")
public class CategoryManagerController {

    @Autowired
    CategoryManagerService categoryManagerService;

    @ResponseBody
    @RequestMapping("list")
    public JSONResponse list(Page<CategoryList> page, String[] codes, String[] cms, int status) {
        page = categoryManagerService.selectPage(page, StringUtils.join(codes,","), StringUtils.join(cms,","), status);
        return new JSONResponse(page);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse save(@Valid CategoryManager categoryManager, BindingResult result, HttpServletRequest request, @SessionUser UserInfo userInfo) {
        if (StringUtils.isEmpty(categoryManager.getId())) {
            categoryManager.setCreateTime(new Date());
            categoryManager.setCreateUser(userInfo.getId());
            categoryManager.setModifyUser(userInfo.getId());
            categoryManager.setModifyTime(new Date());
            categoryManagerService.insert(categoryManager);
        } else {
            categoryManager.setModifyUser(userInfo.getId());
            categoryManager.setModifyTime(new Date());
            categoryManagerService.updateById(categoryManager);
        }
        return new JSONResponse();
    }


}

